(use-modules (zenity)
             (srfi srfi-1))

;; todo.txt should start with this content:
;; ((#f "Wash the dishes") (#f "Tidy the kitchen") (#f "Study science") (#f "Meditate"))

(define (todo-check-off)
  (let* (;; Read in the current todo list
         (todo (call-with-input-file "examples/todo.txt" read))

         ;; Display it as a list
         (checked (or (zenity-checklist "todo" (list "done?" "items") todo
                                        #:height 450)
                      (exit)))

         ;; Remove any checked items off the list to find the unchecked items
         (unchecked (lset-difference equal? (map cadr todo) checked)))

    ;; Save a new version of the todo list out with all the checked off
    ;; items first
    (call-with-output-file "examples/todo.txt"
      (lambda (port)
        (write (append (map (lambda (item) (list #t item)) checked)
                       (map (lambda (item) (list #f item)) unchecked))
               port)
        (newline port)))
    
    (exit)))

(define (todo-add)
  (let* (;; First ask for the user to enter a new todo list item:
         (item (or (zenity-entry "Add todo list item:")
                   (exit)))

         ;; Read in the todo list
         (todo (call-with-input-file "examples/todo.txt" read))
         
         ;; Add this new item to the end of our todo list
         (todo (append todo (list (list #f item)))))
    (call-with-output-file "examples/todo.txt"
      (lambda (port)
        (write todo port)
        (newline port)))
    (todo-option)))

(define (todo-option)
  (if (zenity-question "Add new item? (or press No to check off things you've done)")
      (todo-add)
      (todo-check-off)))

(todo-option)
